home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmpwalk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  4.7 KB  |  164 lines

  1. /*
  2.  * snmpwalk.c - send snmp GETNEXT requests to a network entity, walking a subtree.
  3.  *
  4.  */
  5. /***********************************************************
  6.     Copyright 1988, 1989 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29.  
  30. #include "snmp.h"
  31. #include "snmp_impl.h"
  32. #include "asn1.h"
  33. #include "snmp_api.h"
  34. #include "snmp_client.h"
  35.  
  36. oid objid_mib[] = {1, 3, 6, 1, 2, 1};
  37.  
  38. int    snmp_dump_packet = 0;
  39.  
  40.  
  41. main(argc, argv)
  42.     int        argc;
  43.     char    *argv[];
  44. {
  45.     struct snmp_session    session, *ss;
  46.     struct snmp_pdu *pdu, *response;
  47.     struct variable_list *vars;
  48.     int    arg;
  49.     char *gateway = NULL;
  50.     char *community = NULL;
  51.     int gotroot = 0;
  52.     oid    name[32];
  53.     int name_length;
  54.     oid root[MAX_NAME_LEN];
  55.     int    rootlen, count;
  56.     int running;
  57.     int status;
  58.  
  59.     init_mib();
  60.     /*
  61.      * usage: snmpwalk gateway-name community-name [object-id]
  62.      */
  63.     for(arg = 1; arg < argc; arg++){
  64.     if (argv[arg][0] == '-'){
  65.         switch(argv[arg][1]){
  66.         case 'd':
  67.             snmp_dump_packet++;
  68.             break;
  69.         default:
  70.             printf("invalid option: -%c\n", argv[arg][1]);
  71.             break;
  72.         }
  73.         continue;
  74.     }
  75.     if (gateway == NULL){
  76.         gateway = argv[arg];
  77.     } else if (community == NULL){
  78.         community = argv[arg]; 
  79.     } else {
  80.         rootlen = MAX_NAME_LEN;
  81.         if (read_objid(argv[arg], root, &rootlen)){
  82.         gotroot = 1;
  83.         } else {
  84.         printf("Invalid object identifier: %s\n", argv[arg]);
  85.         }
  86.     }
  87.     }
  88.  
  89.     if (gotroot == 0){
  90.     bcopy((char *)objid_mib, (char *)root, sizeof(objid_mib));
  91.     rootlen = sizeof(objid_mib) / sizeof(oid);
  92.     gotroot = 1;
  93.     }
  94.  
  95.     if (!(gateway && community && gotroot == 1)){
  96.     printf("usage: snmpwalk gateway-name community-name object-identifier\n");
  97.     exit(1);
  98.     }
  99.  
  100.     bzero((char *)&session, sizeof(struct snmp_session));
  101.     session.peername = gateway;
  102.     session.community = (u_char *)community;
  103.     session.community_len = strlen((char *)community);
  104.     session.retries = SNMP_DEFAULT_RETRIES;
  105.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  106.     session.authenticator = NULL;
  107.     snmp_synch_setup(&session);
  108.     ss = snmp_open(&session);
  109.     if (ss == NULL){
  110.     printf("Couldn't open snmp\n");
  111.     exit(-1);
  112.     }
  113.  
  114.     bcopy((char *)root, (char *)name, rootlen * sizeof(oid));
  115.     name_length = rootlen;
  116.  
  117.     running = 1;
  118.     while(running){
  119.     running = 0;
  120.     pdu = snmp_pdu_create(GETNEXT_REQ_MSG);
  121.  
  122.     snmp_add_null_var(pdu, name, name_length);
  123.  
  124.     status = snmp_synch_response(ss, pdu, &response);
  125.     if (status == STAT_SUCCESS){
  126.         if (response->errstat == SNMP_ERR_NOERROR){
  127.         for(vars = response->variables; vars; vars = vars->next_variable){
  128.             if (vars->name_length < rootlen || bcmp(root, vars->name, rootlen * sizeof(oid)))
  129.             continue;    /* not part of this subtree */
  130.             print_variable(vars->name, vars->name_length, vars);
  131.             bcopy((char *)vars->name, (char *)name, vars->name_length * sizeof(oid));
  132.             name_length = vars->name_length;
  133.             running = 1; /* restart so we can get next variable */
  134.         }
  135.         } else {
  136.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  137.             printf("End of MIB.\n");
  138.         } else {
  139.             printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
  140.             if (response->errstat == SNMP_ERR_NOSUCHNAME){
  141.             printf("The request for this object identifier failed: ");
  142.             for(count = 1, vars = response->variables; vars && count != response->errindex;
  143.                 vars = vars->next_variable, count++)
  144.                 ;
  145.             if (vars)
  146.                 print_objid(vars->name, vars->name_length);
  147.             printf("\n");
  148.             }
  149.         }
  150.         }
  151.  
  152.     } else if (status == STAT_TIMEOUT){
  153.         printf("No Response from %s\n", gateway);
  154.     } else {    /* status == STAT_ERROR */
  155.         printf("An error occurred, Quitting\n");
  156.     }
  157.  
  158.     if (response)
  159.         snmp_free_pdu(response);
  160.     }
  161.     snmp_close(ss);
  162. }
  163.  
  164.